package com.xyazm.order.domain.builder;


import com.xyazm.order.domain.dto.*;
import com.xyazm.order.domain.entity.*;
import com.xyazm.order.mapstruct.OrderApiMapstruct;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单详情构造器
 * @author xuyou
 * @date: 2022/6/24 15:20
 */
public class OrderDetailBuilder {

    private OrderDetailDTO orderDetail = new OrderDetailDTO();

    public OrderDetailBuilder orderInfo(OrderInfoDO orderInfoDO) {
        if(orderInfoDO != null) {
            OrderInfoDTO orderInfoDTO = OrderApiMapstruct.INSTANCE.convertOrderInfoDTO(orderInfoDO);
            orderDetail.setOrderInfo(orderInfoDTO);
        }
        return this;
    }

    public OrderDetailBuilder orderItems(List<OrderItemDO> orderItemDOS) {
        if(CollectionUtils.isNotEmpty(orderItemDOS)) {
            List<OrderItemDTO> orderItemDTOList =
                    OrderApiMapstruct.INSTANCE.convertOrderItemDTO(orderItemDOS);
            orderDetail.setOrderItems(orderItemDTOList);
        }
        return this;
    }

    public OrderDetailBuilder orderAmountDetails(List<OrderAmountDetailDO> orderAmountDetailDOS) {
        if(CollectionUtils.isNotEmpty(orderAmountDetailDOS)) {
            List<OrderAmountDetailDTO> orderAmountDetailDTOList =
                    OrderApiMapstruct.INSTANCE.convertOrderAmountDetailDTO(orderAmountDetailDOS);
            orderDetail.setOrderAmountDetails(orderAmountDetailDTOList);
        }
        return this;
    }

    public OrderDetailBuilder orderDeliveryDetail(OrderDeliveryDetailDO orderDeliveryDetailDO) {
       if(orderDeliveryDetailDO != null) {
           OrderDeliveryDetailDTO orderDeliveryDetailDTO =
                   OrderApiMapstruct.INSTANCE.convertOrderDeliveryDetailDTO(orderDeliveryDetailDO);
           orderDetail.setOrderDeliveryDetail(orderDeliveryDetailDTO);
       }
       return this;
    }

    public OrderDetailBuilder orderPaymentDetails(List<OrderPaymentDetailDO> orderPaymentDetailDOS) {
        if(CollectionUtils.isNotEmpty(orderPaymentDetailDOS)) {
            List<OrderPaymentDetailDTO> orderPaymentDetailDTOList =
                    OrderApiMapstruct.INSTANCE.convertOrderPaymentDetailDTO(orderPaymentDetailDOS);
            orderDetail.setOrderPaymentDetails(orderPaymentDetailDTOList);
        }
        return this;
    }

    public OrderDetailBuilder orderAmounts(List<OrderAmountDO> orderAmountDOS) {
        if(CollectionUtils.isNotEmpty(orderAmountDOS)) {
            orderDetail.setOrderAmounts(orderAmountDOS.stream().collect(
                    Collectors.toMap(OrderAmountDO::getAmountType, OrderAmountDO::getAmount, (v1, v2) -> v1)));
        }
        return this;
    }

    public OrderDetailBuilder orderOperateLogs(List<OrderOperateLogDO> orderOperateLogDOS) {
        if(CollectionUtils.isNotEmpty(orderOperateLogDOS)) {
            List<OrderOperateLogDTO> orderOperateLogDTOList =
                    OrderApiMapstruct.INSTANCE.convertOrderOperateLogDTO(orderOperateLogDOS);
            orderDetail.setOrderOperateLogs(orderOperateLogDTOList);
        }
        return this;
    }

    public OrderDetailBuilder orderSnapshots(List<OrderSnapshotDO> orderSnapshotDOS) {
        if(CollectionUtils.isNotEmpty(orderSnapshotDOS)) {
            List<OrderSnapshotDTO> orderSnapshotDTOList =
                    OrderApiMapstruct.INSTANCE.convertOrderSnapshotDTO(orderSnapshotDOS);
            orderDetail.setOrderSnapshots(orderSnapshotDTOList);
        }
        return this;
    }

    public OrderDetailBuilder lackItems(List<OrderLackItemDTO> lackItems) {
        orderDetail.setLackItems(lackItems);
        return this;
    }

    public OrderDetailDTO build() {
        return orderDetail;
    }
}
